package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.XtggDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.base.Xtgg;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午9:57
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XtggService {

    // 系统公告dao
    private XtggDao xtggDao;
    private GsjgDao gsjgDao;

    @Autowired
    public void setXtggDao(XtggDao xtggDao) {
        this.xtggDao = xtggDao;
    }


    /**
     * 查询系统公告
     *
     * @param ggbt
     * @param ggnr
     * @param ggzt
     * @param xtggcjStart
     * @param xtggcjEnd
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllXtgg(String ggbt, String ggnr, String ggzt, Date xtggcjStart, Date xtggcjEnd, int pageNo, int pageSize) {
        Page<Xtgg> xtggPage = this.xtggDao.findXtggList(ggbt, ggnr, ggzt, xtggcjStart, xtggcjEnd, pageNo, pageSize);
        List<Xtgg> xtggList = xtggPage.getResult() == null ? new ArrayList<Xtgg>() : xtggPage.getResult();
        for (int i=0;i<xtggList.size();i++) {
            xtggList.get(i).setGgnr(xtggList.get(i).getGgnr().replaceAll("\r\n",""));
            if(xtggList.get(i).getGgnr().length() > 30){
                xtggList.get(i).setGgnr(xtggList.get(i).getGgnr().substring(0,27) + "...");
            }
        }
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_gsjgList", "jc");
        String xtggJson = BaseUtils.fetchCollectionToJson(xtggList,otherParam, "id","ggbt", "ggnr", "ggzt", "dateCreated", "fbdate","gsjgList","zd","createUser.xm");
        return "{\"total\":" + xtggPage.getTotalCount() + ",\"rows\":[" + xtggJson + "]}";
    }

    public List<Xtgg> findPub() {
        return this.xtggDao.findPub();
    }

    @Transactional
    public void save(Xtgg xtgg) {
        if (xtgg.getId() == null) {
            this.add(xtgg);
        } else {
            this.update(xtgg);
        }
    }

    @Transactional
    private void update(Xtgg xtgg) {
        Xtgg saveData = this.xtggDao.get(xtgg.getId());
        saveData.setGgbt(xtgg.getGgbt());
        saveData.setGgnr(xtgg.getGgnr());
        saveData.setLastUpdated(new Date());
        this.xtggDao.save(saveData);
    }

    @Transactional
    private void add(Xtgg xtgg) {
        xtgg.setCreateUser(YgService.getCurrentYg());
        xtgg.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xtgg.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xtgg.setDateCreated(new Date());
        xtgg.setGgzt("0");
        this.xtggDao.save(xtgg);
    }

    public Xtgg get(Long id) {
        return this.xtggDao.get(id);
    }

    @Transactional
    public void delete(Xtgg xtgg) {
        Xtgg saveData = this.xtggDao.get(xtgg.getId());
        saveData.setDeleted(1);
        this.xtggDao.save(saveData);
    }

    @Transactional
    public void top(Xtgg xtgg,Boolean zd) {
        Xtgg saveData = this.xtggDao.get(xtgg.getId());
        saveData.setZd(zd);
        this.xtggDao.save(saveData);
    }

    @Transactional
    public void publish(Xtgg xtgg,String  gsjgId) {
        Xtgg saveData = this.xtggDao.get(xtgg.getId());
        saveData.setGgzt("1");
        String [] jgids= gsjgId.split(",");
        List<Gsjg> gsjgList = new ArrayList<Gsjg>();
        for(int i = 0;i < jgids.length;i++){
           Gsjg gsjg = gsjgDao.get(Long.parseLong(jgids[i]));
           gsjgList.add(gsjg);
        }
        saveData.setGsjgList(gsjgList);
       // saveData.setCreateUser(YgService.getCurrentYg());
        saveData.setFbdate(new Date());
        this.xtggDao.save(saveData);
    }

    @Transactional
    public void unPublish(Xtgg xtgg) {
        Xtgg saveData = this.xtggDao.get(xtgg.getId());
        saveData.setGgzt("2");
        saveData.setFbdate(null);
        this.xtggDao.save(saveData);
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
